<?php

namespace App\Admin\Controllers\Mental;

use App\Admin\Repositories\Mental\Results;
use App\Admin\Repositories\Mental\Topic;
use App\Models\Mental\Lists;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class TopicController extends AdminController
{
    public function index(Content $content)
    {
        $list = Lists::find($_GET['list_id'] ?? 0);
        return $content
            ->header('评测题目')
            ->description($list->title ?? '')
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Topic(['test']), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $list_id = $_GET['list_id'] ?? 0;
            if (!empty($list_id)) $grid->model()->where('list_id', '=', $list_id);
            $grid->column('id')->sortable();
            $grid->column('test.title', '测评')->filter();
            $grid->column('title');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->model()->setConstraints([
                'list_id' => $list_id,
            ]);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Topic(), function (Show $show) {
            $show->field('id');
            $show->field('list_id');
            $show->field('title');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {

        $builder = \App\Models\Mental\Topic::with('options');
        return Form::make($builder, function (Form $form) {

            $list_id = $_GET['list_id'] ?? $form->input('list_id');
            $form->display('id');
            $form->select('list_id', '评测')->options((new Results())->getOptions())->default($list_id);
            $form->text('title');
            $form->hasMany('options', function (Form\NestedForm $form) {
                $form->text('name');
                $form->text('score', '分数');
            })->label('答案列表');
            $form->disableListButton();
            $form->disableViewButton();
            $form->saved(function (Form $form) use ($list_id) {
                return $form->response()->redirect('mental/topic?list_id=' . $list_id);
            });
            $form->submitted(function ($form) use ($list_id) {
                if (empty($list_id)) { // 你的验证逻辑
                    $form->responseValidationMessages('list_id', '没有选择测评，请从测评列表添加题目');
                }
            });
        });
    }
}
